package xin.yangshuai.oauth201.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import xin.yangshuai.common01.entity.AuthorizationInfo;
import xin.yangshuai.oauth201.config.ImplicitConfig;

@Controller
@RequestMapping("implicit")
public class ImplicitController {

    public static final Logger logger = LoggerFactory.getLogger(ImplicitController.class);

    @Autowired
    private ImplicitConfig implicitConfig;

    @RequestMapping("redirect")
    public String redirect() {

        String authorizeUrl = implicitConfig.getAuthorizeUrl();
        String redirectUrl = implicitConfig.getRedirectUrl();
        String clientId = implicitConfig.getClientId();

        return "redirect:" + authorizeUrl + "?clientId=" + clientId + "&redirectUrl=" + redirectUrl + "&scope=01,02&responseType=token";
    }

    @RequestMapping("callback")
    @ResponseBody
    public AuthorizationInfo callback(AuthorizationInfo authorizationInfo) {

        String accessToken = authorizationInfo.getAccessToken();
        String tokenType = authorizationInfo.getTokenType();
        Integer expiresIn = authorizationInfo.getExpiresIn();

        logger.info("access_token=[{}]", accessToken);
        logger.info("token_type=[{}]", tokenType);
        logger.info("expires_in=[{}]", expiresIn);

        return authorizationInfo;
    }
}
